/**************************************************************
 *@ProjectName: blweibo
 *@FileName: EmotionView.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * The Class EmotionView.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class EmotionView extends LinearLayout implements OnItemClickListener {

    /**
     * The Interface EmotionAdapter.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    public interface EmotionAdapter {

        /**
         * Do action.
         * 
         * @param i
         *            the i
         * @param s
         *            the s
         */
        public abstract void doAction(int i, String s);
    }

    /**
     * The Class GridViewAdapter.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class GridViewAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return list.size();
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int i) {
            return i;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int i, View view, ViewGroup viewgroup) {
            int j = 0;
            int k = ((Integer) getItem(i)).intValue();
            ImageView imageview = null;
            int l;
            if (view == null) {
                Context context = getContext().getApplicationContext();
                imageview = new ImageView(context);
            } else {
                imageview = (ImageView) view;
            }
            imageview.setImageResource(k);
            imageview.setBackgroundResource(R.drawable.bg_face);
            l = getResources().getDimensionPixelSize(R.dimen.emotion_item_view_height);
            imageview.setPadding(j, l, j, l);
            return imageview;
        }

        /** The list. */
        List list;

        /**
         * Instantiates a new grid view adapter.
         * 
         * @param list1
         *            the list1
         */
        public GridViewAdapter(List list1) {
            super();
            list = list1;
        }
    }

    /**
     * Instantiates a new emotion view.
     * 
     * @param context
     *            the context
     */
    public EmotionView(Context context) {
        super(context);
        initViews();
    }

    /**
     * Instantiates a new emotion view.
     * 
     * @param context
     *            the context
     * @param attributeset
     *            the attributeset
     */
    public EmotionView(Context context, AttributeSet attributeset) {
        super(context, attributeset);
        initViews();
    }

    /**
     * Inits the views.
     */
    private void initViews() {
        LayoutInflater.from(getContext()).inflate(R.layout.emotion_main, this);
        mGridView = (GridView) findViewById(R.id.gridView);
        mGridView.setAdapter(new GridViewAdapter(emotionDisplayList));
        mGridView.setOnItemClickListener(this);
    }

    /**
     * Gets the emotion adapter.
     * 
     * @return the emotion adapter
     */
    public EmotionAdapter getEmotionAdapter() {
        return mEmotionAdapter;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
     * .AdapterView, android.view.View, int, long)
     */
    @Override
    public void onItemClick(AdapterView adapterview, View view, int i, long l) {
        if (mEmotionAdapter != null) {
            int j = (emotionDisplayList.get(i)).intValue();
            String s = emotionsKeySrc.get(j);
            mEmotionAdapter.doAction(j, s);
        }
    }

    /**
     * Re build views.
     */
    public void reBuildViews() {
        removeAllViews();
        initViews();
        requestLayout();
        invalidate();
    }

    /**
     * Sets the emotion adapter.
     * 
     * @param emotionadapter
     *            the new emotion adapter
     */
    public void setEmotionAdapter(EmotionAdapter emotionadapter) {
        mEmotionAdapter = emotionadapter;
    }

    /** The Constant emotionDisplayList. */
    private static final ArrayList<Integer> emotionDisplayList;

    /** The Constant emotionsKeySrc. */
    public static final LinkedHashMap<Integer, String> emotionsKeySrc;

    /** The Constant emotionsKeyString. */
    public static final HashMap<String, Integer> emotionsKeyString;

    /** The m emotion adapter. */
    private EmotionAdapter mEmotionAdapter;

    /** The m grid view. */
    private GridView mGridView;
    static {
        emotionDisplayList = new ArrayList<Integer>();
        emotionsKeySrc = new LinkedHashMap<Integer, String>();
        emotionsKeyString = new HashMap<String, Integer>();
        emotionsKeyString.put("眨眼", R.drawable.zayan);
        emotionDisplayList.add(R.drawable.zayan);
        emotionsKeyString.put("微笑", R.drawable.weixiao);
        emotionDisplayList.add(R.drawable.weixiao);
        emotionsKeyString.put("大笑", R.drawable.daxiao);
        emotionDisplayList.add(R.drawable.daxiao);
        emotionsKeyString.put("笑脸", R.drawable.xiaolian);
        emotionDisplayList.add(R.drawable.xiaolian);
        emotionsKeyString.put("白眼", R.drawable.baiyan);
        emotionDisplayList.add(R.drawable.baiyan);
        emotionsKeyString.put("发怒", R.drawable.fanu);
        emotionDisplayList.add(R.drawable.fanu);
        emotionsKeyString.put("鼓掌", R.drawable.guzhang);
        emotionDisplayList.add(R.drawable.guzhang);
        emotionsKeyString.put("偷笑", R.drawable.touxiao);
        emotionDisplayList.add(R.drawable.touxiao);
        emotionsKeyString.put("流泪", R.drawable.liulei);
        emotionDisplayList.add(R.drawable.liulei);
        emotionsKeyString.put("可爱", R.drawable.keai);
        emotionDisplayList.add(R.drawable.keai);
        emotionsKeyString.put("晕", R.drawable.yun);
        emotionDisplayList.add(R.drawable.yun);
        emotionsKeyString.put("流汗", R.drawable.liuhan);
        emotionDisplayList.add(R.drawable.liuhan);
        emotionsKeyString.put("发呆", R.drawable.fadai);
        emotionDisplayList.add(R.drawable.fadai);
        emotionsKeyString.put("害羞", R.drawable.haixiu);
        emotionDisplayList.add(R.drawable.haixiu);
        emotionsKeyString.put("惊讶", R.drawable.jingya);
        emotionDisplayList.add(R.drawable.jingya);
        emotionsKeyString.put("哈哈", R.drawable.haha);
        emotionDisplayList.add(R.drawable.haha);
        emotionsKeyString.put("色色", R.drawable.se);
        emotionDisplayList.add(R.drawable.se);
        emotionsKeyString.put("酷", R.drawable.ku);
        emotionDisplayList.add(R.drawable.ku);
        emotionsKeyString.put("骷髅", R.drawable.kulou);
        emotionDisplayList.add(R.drawable.kulou);
        emotionsKeyString.put("哈欠", R.drawable.haqian);
        emotionDisplayList.add(R.drawable.haqian);
        emotionsKeyString.put("睡觉", R.drawable.shuijiao);
        emotionDisplayList.add(R.drawable.shuijiao);
        emotionsKeyString.put("调皮", R.drawable.tiaopi);
        emotionDisplayList.add(R.drawable.tiaopi);
        emotionsKeyString.put("亲亲", R.drawable.qin);
        emotionDisplayList.add(R.drawable.qin);
        emotionsKeyString.put("疑问", R.drawable.yiwen);
        emotionDisplayList.add(R.drawable.yiwen);
        emotionsKeyString.put("闭嘴", R.drawable.bizui);
        emotionDisplayList.add(R.drawable.bizui);
        emotionsKeyString.put("难过", R.drawable.nanguo);
        emotionDisplayList.add(R.drawable.nanguo);
        emotionsKeyString.put("冷汗", R.drawable.lenghan);
        emotionDisplayList.add(R.drawable.lenghan);
        emotionsKeyString.put("奋斗", R.drawable.fendou);
        emotionDisplayList.add(R.drawable.fendou);
        emotionsKeyString.put("鄙视", R.drawable.bishi);
        emotionDisplayList.add(R.drawable.bishi);
        emotionsKeyString.put("猪头", R.drawable.zhu);
        emotionDisplayList.add(R.drawable.zhu);
        emotionsKeyString.put("爱你", R.drawable.love);
        emotionDisplayList.add(R.drawable.love);
        emotionsKeyString.put("抓狂", R.drawable.crazy);
        emotionDisplayList.add(R.drawable.crazy);
        emotionsKeyString.put("再见", R.drawable.zaijian);
        emotionDisplayList.add(R.drawable.zaijian);
        emotionsKeyString.put("蜡烛", R.drawable.candle);
        emotionDisplayList.add(R.drawable.candle);
        emotionsKeyString.put("蛋糕", R.drawable.cake);
        emotionDisplayList.add(R.drawable.cake);
        emotionsKeyString.put("̫太阳", R.drawable.sun);
        emotionDisplayList.add(R.drawable.sun);
        emotionsKeyString.put("月亮", R.drawable.moon);
        emotionDisplayList.add(R.drawable.moon);
        emotionsKeyString.put("阳光", R.drawable.sunny_thumb);
        emotionDisplayList.add(R.drawable.sunny_thumb);
        emotionsKeyString.put("阴天", R.drawable.dark_thumb);
        emotionDisplayList.add(R.drawable.dark_thumb);
        emotionsKeyString.put("下雨", R.drawable.rain);
        emotionDisplayList.add(R.drawable.rain);
        emotionsKeyString.put("雨伞", R.drawable.umb_thumb);
        emotionDisplayList.add(R.drawable.umb_thumb);
        emotionsKeyString.put("伤心", R.drawable.unheart);
        emotionDisplayList.add(R.drawable.unheart);
        emotionsKeyString.put("心", R.drawable.heart);
        emotionDisplayList.add(R.drawable.heart);
        emotionsKeyString.put("吻", R.drawable.kiss);
        emotionDisplayList.add(R.drawable.kiss);
        emotionsKeyString.put("玫瑰", R.drawable.flower);
        emotionDisplayList.add(R.drawable.flower);
        emotionsKeyString.put("凋谢", R.drawable.diaoxie);
        emotionDisplayList.add(R.drawable.diaoxie);
        emotionsKeyString.put("冰棍", R.drawable.ice);
        emotionDisplayList.add(R.drawable.ice);
        emotionsKeyString.put("咖啡", R.drawable.cafe_thumb);
        emotionDisplayList.add(R.drawable.cafe_thumb);
        emotionsKeyString.put("香茶", R.drawable.kafei);
        emotionDisplayList.add(R.drawable.kafei);
        emotionsKeyString.put("干杯", R.drawable.cheer);
        emotionDisplayList.add(R.drawable.cheer);
        emotionsKeyString.put("酒杯", R.drawable.jiubei);
        emotionDisplayList.add(R.drawable.jiubei);
        emotionsKeyString.put("吃饭", R.drawable.chifan);
        emotionDisplayList.add(R.drawable.chifan);
        emotionsKeyString.put("礼花", R.drawable.bingo_thumb);
        emotionDisplayList.add(R.drawable.bingo_thumb);
        emotionsKeyString.put("电视机", R.drawable.tv_thumb);
        emotionDisplayList.add(R.drawable.tv_thumb);
        emotionsKeyString.put("话筒", R.drawable.m_thumb);
        emotionDisplayList.add(R.drawable.m_thumb);
        emotionsKeyString.put("音乐", R.drawable.music_thumb);
        emotionDisplayList.add(R.drawable.music_thumb);
        emotionsKeyString.put("电影", R.drawable.movie_thumb);
        emotionDisplayList.add(R.drawable.movie_thumb);
        emotionsKeyString.put("唱歌", R.drawable.ktv_thumb);
        emotionDisplayList.add(R.drawable.ktv_thumb);
        emotionsKeyString.put("房子", R.drawable.house_thumb);
        emotionDisplayList.add(R.drawable.house_thumb);
        emotionsKeyString.put("֯织", R.drawable.zz2_thumb);
        emotionDisplayList.add(R.drawable.zz2_thumb);
        emotionsKeyString.put("拥抱", R.drawable.yongbao);
        emotionDisplayList.add(R.drawable.yongbao);
        emotionsKeyString.put("good", R.drawable.good_thumb);
        emotionDisplayList.add(R.drawable.good_thumb);
        emotionsKeyString.put("弱", R.drawable.sad_thumb);
        emotionDisplayList.add(R.drawable.sad_thumb);
        emotionsKeyString.put("不要", R.drawable.no_thumb);
        emotionDisplayList.add(R.drawable.no_thumb);
        emotionsKeyString.put("钱", R.drawable.money_thumb);
        emotionDisplayList.add(R.drawable.money_thumb);
        emotionsKeyString.put("威武", R.drawable.vw_thumb);
        emotionDisplayList.add(R.drawable.vw_thumb);
        emotionsKeyString.put("风扇", R.drawable.fan);
        emotionDisplayList.add(R.drawable.fan);
        emotionsKeyString.put("囧", R.drawable.j_thumb);
        emotionDisplayList.add(R.drawable.j_thumb);
        emotionsKeyString.put("打针", R.drawable.zt_thumb);
        emotionDisplayList.add(R.drawable.zt_thumb);
        emotionsKeyString.put("黄牌", R.drawable.yellowcard);
        emotionDisplayList.add(R.drawable.yellowcard);
        emotionsKeyString.put("红牌", R.drawable.redcard);
        emotionDisplayList.add(R.drawable.redcard);
        emotionsKeyString.put("帅", R.drawable.s2_thumb);
        emotionDisplayList.add(R.drawable.s2_thumb);
        emotionsKeyString.put("发言", R.drawable.fayan);
        emotionDisplayList.add(R.drawable.fayan);
        emotionsKeyString.put("粉", R.drawable.fen);
        emotionDisplayList.add(R.drawable.fen);
        emotionsKeyString.put("亮相", R.drawable.liangxiang);
        emotionDisplayList.add(R.drawable.liangxiang);
        emotionsKeyString.put("礼物", R.drawable.liwu);
        emotionDisplayList.add(R.drawable.liwu);
        emotionsKeyString.put("围脖", R.drawable.wb_s);
        emotionDisplayList.add(R.drawable.wb_s);
        emotionsKeyString.put("坏笑", R.drawable.huaixiao_thumb);
        emotionDisplayList.add(R.drawable.huaixiao_thumb);
        emotionsKeyString.put("浮云", R.drawable.fuyun_thumb);
        emotionDisplayList.add(R.drawable.fuyun_thumb);
        emotionsKeyString.put("给力", R.drawable.geili_thumb);
        emotionDisplayList.add(R.drawable.geili_thumb);
        emotionsKeyString.put("神马", R.drawable.horse_thumb);
        emotionDisplayList.add(R.drawable.horse_thumb);
        emotionsKeyString.put("互粉", R.drawable.hufen_thumb);
        emotionDisplayList.add(R.drawable.hufen_thumb);
        emotionsKeyString.put("超人", R.drawable.superman);
        emotionDisplayList.add(R.drawable.superman);
        Iterator iterator = emotionsKeyString.keySet().iterator();
        while (iterator.hasNext()) {
            String str = (String) iterator.next();
            int j = emotionsKeyString.get(str);
            emotionsKeySrc.put(j, str);
        }
    }
}
